package com.mobile.util;

import org.apache.ibatis.type.Alias;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName : PageData.java
 * @Decription : 参数封装Map
 * @Version : 2018/01/11  hlh  新建作成   1.0
 */
@SuppressWarnings("rawtypes")
@Alias("pd")
public class PageData extends HashMap implements Map{

	private static final long serialVersionUID = 1L;

	Map map = null;
	HttpServletRequest request;
	@SuppressWarnings({"unchecked"})
	public PageData(HttpServletRequest request){
		this.request = request;
		Map properties = request.getParameterMap();
		Map returnMap = new HashMap();
		Iterator entries = properties.entrySet().iterator();
		Entry entry;
		String name = "";
		String value = "";
		while (entries.hasNext()) {
			entry = (Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if(null == valueObj){
				value = "";
			}else if(valueObj instanceof String[]){
				String[] values = (String[])valueObj;
				for(int i=0;i<values.length;i++){
					 value = values[i] + ",";
				}
				value = value.substring(0, value.length()-1);
			}else{
				value = valueObj.toString();
			}
			returnMap.put(name, value);
		}
		map = returnMap;
	}

	public PageData() {
		map = new HashMap();
	}

	@Override
	public Object get(Object key) {
		Object obj = null;
		if(map.get(key) instanceof Object[]) {
			Object[] arr = (Object[])map.get(key);
			obj = request == null ? arr:(request.getParameter((String)key) == null ? arr:arr[0]);
		} else {
			obj = map.get(key);
		}
		return obj;
	}

	public String getString(Object key) {
		return (String)get(key);
	}

	/**
	 * 获取 Boolean 类型的字段值
	 *
	 * @param key 键
	 * @return Boolean 类型的值，如果键不存在或无法转换为 Boolean，则返回 null
	 */
	public Boolean getBoolean(Object key) {
		Object value = map.get(key);
		if (value instanceof Boolean) {
			return (Boolean) value;
		} else if (value instanceof String) {
			return Boolean.parseBoolean((String) value);
		}
		return null;
	}


	/**
	 * 获取 LocalDateTime 类型的字段值
	 *
	 * @param key 键
	 * @return LocalDateTime 类型的值，如果键不存在或无法解析日期格式，则返回 null
	 */
	public LocalDateTime getLocalDateTime(Object key) {
		Object value = map.get(key);
		if (value == null) {
			return null;
		}

		if (value instanceof LocalDateTime) {
			return (LocalDateTime) value;
		} else if (value instanceof String) {
			try {
				// 尝试使用常见的日期时间格式进行解析，如 "yyyy-MM-dd HH:mm:ss"
				return LocalDateTime.parse((String) value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
			} catch (Exception e) {
				// 如果格式不匹配，也可以支持其他格式，或者返回 null 表示解析失败
				return null;
			}
		}
		return null;
	}

	/**
	 * 获取 Integer 类型的字段值
	 *
	 * @param key 键
	 * @return Integer 类型的值，如果键不存在或无法转换为 Integer，则返回 null
	 */
	public Integer getInteger(Object key) {
		Object value = map.get(key);
		if (value instanceof Integer) {
			return (Integer) value;
		} else if (value instanceof String) {
			try {
				return Integer.parseInt((String) value);
			} catch (NumberFormatException e) {
				return null;
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object put(Object key, Object value) {
		return map.put(key, value);
	}

	@Override
	public Object remove(Object key) {
		return map.remove(key);
	}

	public void clear() {
		map.clear();
	}

	public boolean containsKey(Object key) {
		return map.containsKey(key);
	}

	public boolean containsValue(Object value) {
		return map.containsValue(value);
	}

	public Set entrySet() {
		return map.entrySet();
	}

	public boolean isEmpty() {
		return map.isEmpty();
	}

	public Set keySet() {
		return map.keySet();
	}

	@SuppressWarnings("unchecked")
	public void putAll(Map t) {
		map.putAll(t);
	}

	public int size() {
		return map.size();
	}

	public Collection values() {
		return map.values();
	}
}
